Completed
Push — js-scrutinizing ( fe8d2d...ccc639 )
by Maxence
02:15
created

$(document).ready   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 1
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
/** global: Notyf */
29
30
var api = OCA.Circles.api;
31
var curr = {
32
	circlesType: '',
33
	circle: 0,
34
	circleLevel: 0,
35
	searchCircle: '',
36
	searchUser: ''
37
};
38
39
40
var nav = {
41
42
	displayCirclesList: function (type) {
43
44
		curr.circlesType = type;
45
		curr.searchCircle = '';
46
		curr.searchUser = '';
47
48
		curr.circle = 0;
49
		curr.circleLevel = 0;
50
51
		navdiv.navigation.show('slide', 800);
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
		navdiv.emptyContent.show(800);
53
		navdiv.mainUI.fadeOut(800);
54
55
		$('#circles_search').val('');
56
		$('#addmember').val('');
57
58
		this.UIresetCirclesList(type);
59
		api.listCircles(type, self.listCirclesResult);
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
60
	},
61
62
	selectCircle: function (circle_id) {
63
		curr.searchUser = '';
64
		navdiv.addMember.val('');
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
65
66
		api.detailsCircle(circle_id, this.selectCircleResult);
67
	}
68
69
70
};
71
72
73
$(document).ready(function () {
74
75
	/**
76
	 * @constructs Navigation
77
	 */
78
79
80
81
	var Navigation = function () {
82
83
			$.extend(Navigation.prototype, curr);
84
			$.extend(Navigation.prototype, nav);
85
			$.extend(Navigation.prototype, navdiv);
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
86
			$.extend(Navigation.prototype, actions);
0 ignored issues
show
Bug introduced by
The variable actions seems to be never declared. If this is a global, consider adding a /** global: actions */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
87
88
			this.initialize();
89
90
		} || {};
91
92
93
	Navigation.prototype = {
94
95
96
		initialize: function () {
97
98
			var self = this;
99
100
			navdiv.initElements();
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
101
			navdiv.initUI();
102
103
			//
104
			// /**
105
			//  *
106
			//  * @constructor
107
			//  */
108
			// this.UIReset = function () {
109
			// 	navdiv.newTypeDefinition.children('div').fadeOut(0);
110
			// 	$('#circles_new_type_' +
111
			// navdiv.newType.children('option:selected').val()).fadeIn( 0);
112
			// navdiv.newType.hide(); navdiv.newSubmit.hide(); navdiv.newTypeDefinition.hide();
113
			// $('.icon-circles').css('background-image', 'url(' + OC.imagePath('circles',
114
			// 'colored') + ')');  navdiv.membersSearchResult.hide(); };
115
116
			/**
117
			 *
118
			 */
119
			this.initTweaks = function () {
120
				$.fn.emptyTable = function () {
121
					this.children('tr').each(function () {
122
						if ($(this).attr('class') != 'header') {
123
							$(this).remove();
124
						}
125
					});
126
				};
127
128
129
			};
130
131
132
			//
133
			// this.displayMembersInteraction = function (details) {
134
			// 	if (details.user.level < 6) {
135
			// 		divAddMember.hide();
136
			// 	} else {
137
			// 		divAddMember.show();
138
			// 	}
139
			//
140
			// 	this.displayNonMemberInteraction(details);
141
			//
142
			// 	if (details.user.level == 9) {
143
			// 		navdiv.joinCircle.hide();
144
			// 		navdiv.leaveCircle.hide();
145
			// 		return;
146
			// 	}
147
			//
148
			// 	if (details.user.level >= 1) {
149
			// 		navdiv.joinCircle.hide();
150
			// 		navdiv.leaveCircle.show();
151
			// 	}
152
			//
153
			// };
154
155
156
			this.displayNonMemberInteraction = function (details) {
157
				navdiv.joinCircleAccept.hide();
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
158
				navdiv.joinCircleReject.hide();
159
				navdiv.joinCircleRequest.hide();
160
				navdiv.joinCircleInvite.hide();
161
162
				if (details.user.status == 'Invited') {
163
					navdiv.joinCircleInvite.show();
164
					navdiv.joinCircleAccept.show();
165
					navdiv.joinCircleReject.show();
166
					navdiv.joinCircle.hide();
167
					navdiv.leaveCircle.hide();
168
					return;
169
				}
170
171
				if (details.user.status == 'Requesting') {
172
					navdiv.joinCircleRequest.show();
173
					navdiv.joinCircle.hide();
174
					navdiv.leaveCircle.show();
175
					return;
176
				}
177
178
				navdiv.joinCircle.show();
179
				navdiv.leaveCircle.hide();
180
			};
181
182
183
			// /**
184
			//  *
185
			//  */
186
			// this.initAnimationNewCircle = function () {
187
			//
188
			// 	navdiv.newName.on('keyup', function () {
189
			// 		self.onEventNewCircleName();
190
			// 	});
191
			//
192
			// 	navdiv.newType.on('change', function () {
193
			// 		self.onEventNewCircleType();
194
			// 	});
195
			//
196
			// 	navdiv.newSubmit.on('click', function () {
197
			// 		api.createCircle(navdiv.newType.val(), navdiv.newName.val(),
198
			// 			self.createCircleResult);
199
			// 	});
200
			//
201
			// };
202
203
204
			/**
205
			 *
206
			 * @param display
207
			 */
208
			this.displayOptionsNewCircle = function (display) {
209
				if (display) {
210
					navdiv.newType.fadeIn(300);
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
211
					navdiv.newSubmit.fadeIn(500);
212
					navdiv.newTypeDefinition.fadeIn(700);
213
				}
214
				else {
215
					navdiv.newType.fadeOut(700);
216
					navdiv.newSubmit.fadeOut(500);
217
					navdiv.newTypeDefinition.fadeOut(300);
218
				}
219
			};
220
221
222
			/**
223
			 *
224
			 */
225
			this.initExperienceCirclesList = function () {
226
227
				navdiv.circlesList.children('div').on('click', function () {
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
228
					self.displayCirclesList($(this).attr('circle-type'));
229
				});
230
231
				$('#circles_search').on('input propertychange paste focus', function () {
232
					if (curr.searchCircle == $(this).val().trim()) {
233
						return;
234
					}
235
236
					curr.searchCircle = $(this).val().trim();
237
					api.searchCircles(curr.circlesType, $(this).val().trim(),
238
						self.listCirclesResult);
239
				});
240
241
			};
242
243
244
			/**
245
			 *
246
			 * @param type
247
			 */
248
			this.displayCirclesList = function (type) {
249
250
				curr.circlesType = type;
251
				curr.searchCircle = '';
252
				curr.searchUser = '';
253
254
				curr.circle = 0;
255
				curr.circleLevel = 0;
256
257
				navdiv.navigation.show('slide', 800);
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
258
				navdiv.emptyContent.show(800);
259
				navdiv.mainUI.fadeOut(800);
260
261
				$('#circles_search').val('');
262
				$('#addmember').val('');
263
264
				this.UIresetCirclesList(type);
265
				api.listCircles(type, self.listCirclesResult);
266
			};
267
268
269
			/**
270
			 *
271
			 * @constructor
272
			 */
273
			this.UIresetCirclesList = function (type) {
274
275
				navdiv.circlesList.children('div').removeClass('selected');
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
276
				navdiv.circlesList.children().each(function () {
277
					if ($(this).attr('circle-type') == type.toLowerCase()) {
278
						$(this).addClass('selected');
279
					}
280
				});
281
282
				navdiv.navigation.addClass('selected');
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
283
				navdiv.navigation.children().each(function () {
284
					if ($(this).attr('id') != 'circles_search') {
285
						$(this).remove();
286
					}
287
				});
288
			};
289
290
291
			/**
292
			 *
293
			 * @param result
294
			 */
295
			this.listCirclesResult = function (result) {
296
297
				if (result.status < 1) {
298
					OCA.notification.onFail(
299
						'Issue while retreiving the list of the Circles: ' +
300
						((result.error) ? result.error : 'no error message'));
301
					return;
302
				}
303
304
				var data = result.data;
305
				for (var i = 0; i < data.length; i++) {
306
					var tmpl = self.generateTmplCircle(data[i]);
307
					navdiv.navigation.append(
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
308
						'<div class="circle" circle-id="' + data[i].id + '">' + tmpl +
309
						'</div>');
310
				}
311
312
				navdiv.navigation.children('.circle').on('click', function () {
313
					self.selectCircle($(this).attr('circle-id'));
314
				});
315
			};
316
317
318
			/**
319
			 *
320
			 * @returns {*|jQuery}
321
			 * @param entry
322
			 */
323
			this.generateTmplCircle = function (entry) {
324
				var tmpl = $('#tmpl_circle').html();
325
326
				tmpl = tmpl.replace(/%title%/, entry.name);
327
				tmpl = tmpl.replace(/%type%/, entry.type);
328
				tmpl = tmpl.replace(/%owner%/, entry.owner.user_id);
329
				tmpl = tmpl.replace(/%status%/, entry.user.status);
330
				tmpl = tmpl.replace(/%level_string%/, entry.user.level_string);
331
				tmpl = tmpl.replace(/%count%/, entry.count);
332
				tmpl = tmpl.replace(/%creation%/, entry.creation);
333
334
				return tmpl;
335
			};
336
337
			//
338
			// /**
339
			//  *
340
			//  * @param circle_id
341
			//  */
342
			// this.selectCircle = function (circle_id) {
343
			// 	curr.searchUser = '';
344
			// 	$('#addmember').val('');
345
			//
346
			// 	api.detailsCircle(circle_id, this.selectCircleResult);
347
			// };
348
			//
349
350
			this.selectCircleResult = function (result) {
351
352
				navdiv.mainUIMembers.emptyTable();
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
353
354
				if (result.status < 1) {
355
					OCA.notification.onFail(
356
						'Issue while retreiving the details of a circle: ' +
357
						((result.error) ? result.error : 'no error message'));
358
					return;
359
				}
360
361
				navdiv.navigation.children('.circle').removeClass('selected');
362
				navdiv.navigation.children(".circle[circle-id='" + result.circle_id + "']").each(
363
					function () {
364
						$(this).addClass('selected');
365
					});
366
367
				navdiv.emptyContent.hide(800);
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
368
				navdiv.mainUI.fadeIn(800);
369
				curr.circle = result.circle_id;
370
				curr.circleLevel = result.details.user.level;
371
372
				self.displayMembersInteraction(result.details);
373
				self.displayMembers(result.details.members);
374
			};
375
376
			//
377
			// /**
378
			//  *
379
			//  * @param search
380
			//  */
381
			// this.searchMembersRequest = function (search) {
382
			//
383
			// 	if (curr.searchUser == search) {
384
			// 		return;
385
			// 	}
386
			//
387
			// 	curr.searchUser = search;
388
			//
389
			// 	$.get(OC.linkToOCS('apps/files_sharing/api/v1', 1) + 'sharees',
390
			// 		{
391
			// 			format: 'json',
392
			// 			search: search,
393
			// 			perPage: 200,
394
			// 			itemType: 'principals'
395
			// 		}, self.searchMembersResult);
396
			// };
397
			//
398
			//
399
			// this.searchMembersResult = function (response) {
400
			//
401
			// 	if (response === null ||
402
			// 		(response.ocs.data.users === 0 && response.ocs.data.exact.users === 0)) {
403
			// 		navdiv.membersSearchResult.fadeOut(300);
404
			// 		return;
405
			// 	}
406
			//
407
			// 	navdiv.membersSearchResult.children().remove();
408
			//
409
			// 	self.searchMembersResultFill(response.ocs.data.exact.users,
410
			// 		response.ocs.data.users);
411
			//
412
			// 	$('.members_search').on('click', function () {
413
			// 		api.addMember(curr.circle, $(this).attr('searchresult'),
414
			// 			self.addMemberResult);
415
			// 	});
416
			// 	navdiv.membersSearchResult.fadeIn(300);
417
			//
418
			// };
419
420
421
			// this.searchMembersResultFill = function (exact, partial) {
422
			// 	$.each(exact, function (index, value) {
423
			// 		navdiv.membersSearchResult.append(
424
			// 			'<div class="members_search exact" searchresult="' +
425
			// 			value.value.shareWith + '">' + value.label + '   (' +
426
			// 			value.value.shareWith + ')</div>');
427
			// 	});
428
			//
429
			// 	$.each(partial, function (index, value) {
430
			// 		var currSearch = $('#addmember').val().trim();
431
			// 		var line = value.label + '   (' + value.value.shareWith + ')';
432
			// 		if (currSearch.length > 0) {
433
			// 			line =
434
			// 				line.replace(new RegExp('(' + currSearch + ')', 'gi'),
435
			// 					'<b>$1</b>');
436
			// 		}
437
			//
438
			// 		navdiv.membersSearchResult.append(
439
			// 			'<div class="members_search" searchresult="' +
440
			// 			value.value.shareWith +
441
			// 			'">' + line + '</div>');
442
			// 	});
443
			//
444
			// 	navdiv.membersSearchResult.children().first().css('border-top-width', '0px');
445
			// };
446
447
448
			// /**
449
			//  *
450
			//  * @param result
451
			//  */
452
			// this.addMemberResult = function (result) {
453
			//
454
			// 	if (result.status == 1) {
455
			// 		OCA.notification.onSuccess(
456
			// 			"Member '" + result.name + "' successfully added to the circle");
457
			//
458
			// 		self.displayMembers(result.members);
459
			// 		return;
460
			// 	}
461
			// 	OCA.notification.onFail(
462
			// 		"Member '" + result.name + "' NOT added to the circle: " +
463
			// 		((result.error) ? result.error : 'no error message'));
464
			// };
465
466
467
			this.generateTmplMember = function (entry) {
468
				var tmpl = $('#tmpl_member').html();
469
470
				tmpl = tmpl.replace(/%username%/g, entry.user_id);
471
				tmpl = tmpl.replace(/%level%/g, entry.level);
472
				tmpl = tmpl.replace(/%levelstring%/g, entry.level_string);
473
				tmpl = tmpl.replace(/%status%/, entry.status);
474
				tmpl = tmpl.replace(/%joined%/, entry.joined);
475
				tmpl = tmpl.replace(/%note%/, entry.note);
476
477
				return tmpl;
478
			};
479
480
481
482
483
484
			this.joinCircleResult = function (result) {
485
				if (result.status == 1) {
486
487
					navdiv.mainUIMembers.children("[member-id='" + result.name + "']").each(
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
488
						function () {
489
							$(this).hide(300);
490
						});
491
492
					if (result.member.level == 1) {
493
						OCA.notification.onSuccess(
494
							"You have successfully joined this circle");
495
					} else {
496
						OCA.notification.onSuccess(
497
							"You have requested an invitation to join this circle");
498
					}
499
					self.selectCircle(result.circle_id);
500
					return;
501
				}
502
503
				OCA.notification.onFail(
504
					"Cannot join this circle: " +
505
					((result.error) ? result.error : 'no error message'));
506
507
			};
508
509
			this.leaveCircleResult = function (result) {
510
				if (result.status == 1) {
511
512
					navdiv.mainUIMembers.children("[member-id='" + result.name + "']").each(
0 ignored issues
show
Bug introduced by
The variable navdiv seems to be never declared. If this is a global, consider adding a /** global: navdiv */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
513
						function () {
514
							$(this).hide(300);
515
						});
516
517
					self.selectCircle(result.circle_id);
518
					OCA.notification.onSuccess(
519
						"You have successfully left this circle");
520
					return;
521
				}
522
523
				OCA.notification.onFail(
524
					"Cannot leave this circle: " +
525
					((result.error) ? result.error : 'no error message'));
526
527
			};
528
529
530
			/**
531
			 * Inits
532
			 */
533
			this.initTweaks();
534
		//	this.UIReset();
535
			this.initAnimationNewCircle();
536
			this.initExperienceCirclesList();
537
			// this.initExperienceMembersManagment();
538
		}
539
	};
540
541
542
	/**
543
	 * @constructs Notification
544
	 */
545
	var Notification = function () {
546
		this.initialize();
547
	};
548
549
	Notification.prototype = {
550
551
		initialize: function () {
552
553
			//noinspection SpellCheckingInspection
554
			var notyf = new Notyf({
555
				delay: 5000
556
			});
557
558
			this.onSuccess = function (text) {
559
				notyf.confirm(text);
560
			};
561
562
			this.onFail = function (text) {
563
				notyf.alert(text);
564
			};
565
566
		}
567
568
	};
569
570
	OCA.Circles.Navigation = Navigation;
571
	OCA.Circles.navigation = new Navigation();
572
573
	OCA.Notification = Notification;
574
	OCA.notification = new Notification();
575
576
});
577
578